(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid = userinfo.user_id;
	var c1 = 60;
	var c2 = 60;
	var area_code=userinfo.area_code;

	$.plusReady(function() {
		var device=get_device();
		
		function get_device() {
			var device = {
				IMEI: plus.device.imei,
				IMSI: '',
				Model: plus.device.model,
				Vendor: plus.device.vendor,
				UUID: plus.device.uuid,
				Screen: plus.screen.resolutionWidth * plus.screen.scale + 'x' + plus.screen.resolutionHeight * plus.screen.scale + '',
				DPI: plus.screen.dpiX + 'x' + plus.screen.dpiY,
				OS: new Object()
			};
			for(var i = 0; i < plus.device.imsi.length; i++) {
				device.IMSI += plus.device.imsi[i];
			}
			var types = {};
			types[plus.networkinfo.CONNECTION_UNKNOW] = '未知网络';
			types[plus.networkinfo.CONNECTION_NONE] = '未连接网络';
			types[plus.networkinfo.CONNECTION_ETHERNET] = '有线网络';
			types[plus.networkinfo.CONNECTION_WIFI] = 'WiFi网络';
			types[plus.networkinfo.CONNECTION_CELL2G] = '2G蜂窝网络';
			types[plus.networkinfo.CONNECTION_CELL3G] = '3G蜂窝网络';
			types[plus.networkinfo.CONNECTION_CELL4G] = '4G蜂窝网络';
			device.NetworkInfo = types[plus.networkinfo.getCurrentType()];
			device.OS = {
				Language: plus.os.language,
				Version: plus.os.version,
				Name: plus.os.name,
				Vendor: plus.os.vendor
			};
			return device;
	};
	
	   if(!device.UUID){
			device.UUID=Math.floor(Math.random() * 1000000000000000 + 100000000000000).toString();
		}
	    console.log(JSON.stringify(device.UUID));
		//旧手机号 点击获取验证码
		doc.getElementById("old_getCode").addEventListener('tap', function() {
			if(c1!=60){
				return;
			}
			do_blur();
			
			var	phone=doc.getElementById('old_phone').value.trim();
			if(!phone || (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) && area_code=='86')) {
				plus.nativeUI.toast("旧手机号格式不正确");
				return;
			}
			
			var Info={'user_id':uid,'phone': phone,'etype':2,'_token':userinfo.token,'uuid':device.UUID};
			console.log(JSON.stringify(Info));
			var btnArray = ['确定', '取消'];
			mui.confirm('我们将发送验证码短信到下面的号码：\n' + phone, '确认手机号码', btnArray, function(e) {
				if(e.index == 0) {

					mui.get(apiurl + "User/getsms/",Info, function(res) {
						console.log(JSON.stringify(res));
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}
						
						if(res.status == 1) {
							//发送短信成功
							//localStorage.setItem('_send_code', res.ck_code);
							plus.nativeUI.toast("短信验证码已发送，请注意查收");
						} else {
							plus.nativeUI.toast(res.msg);
							document.getElementById('old_getCode').innerHTML = '获取验证码';
							c1 = 60;
							return;
						}
						var code1 = setInterval(function() {
							document.getElementById('old_getCode').innerHTML =c1 + '秒';
							c1 = c1 - 1;
							if(c1 >= 0) {
								
							} else {
								document.getElementById('old_getCode').innerHTML = '获取验证码';
								c1 = 60;
								clearInterval(code1);
							}
						}, 1000);
					});

				}
			})
     });
     
     
     //新手机号  点击获取验证码
		doc.getElementById("getCode").addEventListener('tap', function() {
			if(c2!=60){
				return;
			}
			do_blur();
			
			var	phone=doc.getElementById('phone').value.trim();
			if(!phone || (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) && area_code=='86')) {
				plus.nativeUI.toast("新手机号格式不正确");
				return;
			}
			
			if(doc.getElementById('old_phone').value.trim()==doc.getElementById('phone').value.trim()){
				plus.nativeUI.toast("旧手机号与新手机号相同");
				return;
			}
			
			var Info={'user_id':uid,'phone': phone,'etype':3,'_token':userinfo.token,'uuid':device.UUID};
			console.log(JSON.stringify(Info));
			var btnArray = ['确定', '取消'];
			mui.confirm('我们将发送验证码短信到下面的号码：\n' + phone, '确认手机号码', btnArray, function(e) {
				if(e.index == 0) {

					mui.get(apiurl + "User/getsms/",Info, function(res) {
						console.log(JSON.stringify(res));
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}

						if(res.status == 1) {
							//发送短信成功
							//localStorage.setItem('_send_code', res.ck_code);
							plus.nativeUI.toast("短信验证码已发送，请注意查收");
						} else {
							plus.nativeUI.toast(res.msg);
							document.getElementById('getCode').innerHTML = '获取验证码';
							c2 = 60;
							return;
						}
						var code1 = setInterval(function() {
							document.getElementById('getCode').innerHTML =c2 + '秒';
							c2 = c2 - 1;
							if(c2 >= 0) {
								
							} else {
								document.getElementById('getCode').innerHTML = '获取验证码';
								c2 = 60;
								clearInterval(code1);
							}
						}, 1000);
					});

				}
			})
     });
     
        function do_blur(){
        	doc.getElementById('old_phone').blur();
        	doc.getElementById('old_code').blur();
        	doc.getElementById('phone').blur();
        	doc.getElementById('code').blur();
        }
     
    //提交
    doc.getElementById("do_set").addEventListener('tap', function() {
    	do_blur();
    	var Info={
    		user_id: uid,
    		old_phone: doc.getElementById('old_phone').value.trim(),
    		old_code: doc.getElementById('old_code').value.trim(),
    		phone: doc.getElementById('phone').value.trim(),
    		code: doc.getElementById('code').value.trim(),
    		_token:userinfo.token
    	};
    	if(!Info.old_phone || (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(Info.old_phone)) && area_code=='86')) {
			plus.nativeUI.toast("旧手机号格式不正确");
			return;
		}
    	
    	if(!Info.old_code) {
			plus.nativeUI.toast("验证码不能为空");
			return;
		}
    	
    	if(!Info.phone || (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(Info.phone)) && area_code=='86')) {
			plus.nativeUI.toast("新手机号格式不正确");
			return;
		}
    	
    	if(Info.old_phone==Info.phone) {
			plus.nativeUI.toast("新手机号与旧手机号相同");
			return;
		}
    	if(!Info.code) {
			plus.nativeUI.toast("验证码不能为空");
			return;
		}
    	mui.ajax({
				url: apiurl + "User/set_phone/",
				data: Info,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					
					// 请求成功
					if(res.status ==0) {
						mui.toast(res.msg);
					} else {
						mui.toast('更换手机号成功');
						userinfo.mobile=Info.phone;
						app.setState(userinfo);
	
                        mui.back();
						
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
    });
     
     
  })
	
}(mui, document));